import type { TableColumnProps } from 'ant-design-vue'
export interface IColumnType extends TableColumnProps {
  dataIndex?: string | string[];
  width?: number | string;
  boolean?: boolean;
}

const MAP_WIDTH: Record<string, number> = {
  vin: 300
}

export function widthDataIndex (dataIndex: string | string[] | undefined) {
  if (dataIndex === undefined) {
    return ''
  }
  return Array.isArray(dataIndex) ? dataIndex.join('.') : dataIndex
}

export function createColumn(dataIndex: string | string[], title: string, slots?: boolean): IColumnType
export function createColumn(dataIndex: string | string[], title: string, config: TableColumnProps, slots?: boolean): IColumnType
export function createColumn(dataIndex: string | string[], title: string, width: number, slots?: boolean): IColumnType
export function createColumn(dataIndex: string | string[], title: string, config: TableColumnProps, width: number, slots?: boolean): IColumnType
export function createColumn (
  dataIndex: string | string[],
  title: string,
  ...args: any[]): IColumnType {
  const column = {} as IColumnType
  let [config, width] = args

  if (typeof config === 'object') {
    Object.assign(column, config)
  } else if (typeof config === 'number') {
    width = config
  }

  if (MAP_WIDTH[widthDataIndex(dataIndex)]) {
    width = MAP_WIDTH[widthDataIndex(dataIndex)]
  }

  if (typeof width === 'number') {
    column.width = width
  }

  if (typeof args[args.length - 1] === 'boolean' && args[args.length - 1]) {
    column.key = widthDataIndex(dataIndex)
  }

  return {
    title,
    dataIndex,
    ...column
  } as IColumnType
}
